package com.soulkun.smarthome.logic.manager

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.Parcelable
import androidx.localbroadcastmanager.content.LocalBroadcastManager
import com.soulkun.smarthome.logic.Constant
import com.soulkun.smarthome.logic.dto.AlertMessageDTO
import com.soulkun.smarthome.logic.dto.FamilyMessageDTO
import com.soulkun.smarthome.logic.dto.StatusMessageDTO
import com.soulkun.smarthome.logic.dto.UserMessageDTO
import com.soulkun.smarthome.logic.enum.MessageCodeEnum
import soulkun.library.utils.logE
import soulkun.library.utils.toast

object BroadcastManager {

    class SoulkunBroadcastReceiver(private val messageCodeEnum: MessageCodeEnum, private val onReceiveListener: (Parcelable) -> Unit) : BroadcastReceiver() {
        override fun onReceive(context: Context?, intent: Intent?) {
            when (messageCodeEnum) {
                MessageCodeEnum.Status -> {
                    intent?.getParcelableExtra<StatusMessageDTO>(Constant.BUNDLE_MESSAGE_STATUS)?.let {
                        onReceiveListener(it)
                    }       
                }
                MessageCodeEnum.Alert -> {
                    intent?.getParcelableExtra<AlertMessageDTO>(Constant.BUNDLE_MESSAGE_ALERT)?.let {
                        onReceiveListener(it)
                    }
                }
                MessageCodeEnum.Family -> {
                    intent?.getParcelableExtra<FamilyMessageDTO>(Constant.BUNDLE_MESSAGE_FAMILY)?.let {
                        onReceiveListener(it)
                    }
                }
                MessageCodeEnum.User -> {
                    intent?.getParcelableExtra<UserMessageDTO>(Constant.BUNDLE_MESSAGE_USER)?.let {
                        onReceiveListener(it)
                    }
                }
            }
        }
    }

    fun sendBroadcast(context: Context, messageCodeEnum: MessageCodeEnum, data: Parcelable) {
        when (messageCodeEnum) {
            MessageCodeEnum.Status -> {
                LocalBroadcastManager.getInstance(context).sendBroadcast(Intent(Constant.BROADCAST_MESSAGE_STATUS).apply {
                    putExtra(Constant.BUNDLE_MESSAGE_STATUS, data)
                })
            }
            MessageCodeEnum.Alert -> {
                LocalBroadcastManager.getInstance(context).sendBroadcast(Intent(Constant.BROADCAST_MESSAGE_ALERT).apply {
                    putExtra(Constant.BUNDLE_MESSAGE_ALERT, data)
                })
            }
            MessageCodeEnum.Family -> {
                LocalBroadcastManager.getInstance(context).sendBroadcast(Intent(Constant.BROADCAST_MESSAGE_FAMILY).apply {
                    putExtra(Constant.BUNDLE_MESSAGE_FAMILY, data)
                })
            }
            MessageCodeEnum.User -> {
                LocalBroadcastManager.getInstance(context).sendBroadcast(Intent(Constant.BROADCAST_MESSAGE_USER).apply {
                    putExtra(Constant.BUNDLE_MESSAGE_USER, data)
                })
            }
        }
    }

    fun registerBroadcast(
        context: Context,
        messageCodeEnum: MessageCodeEnum,
        broadcastReceiver: BroadcastReceiver
    ) {
        val intentFilter = when (messageCodeEnum) {
            MessageCodeEnum.Status -> {
                IntentFilter(Constant.BROADCAST_MESSAGE_STATUS)
            }
            MessageCodeEnum.Alert -> {
                IntentFilter(Constant.BROADCAST_MESSAGE_ALERT)
            }
            MessageCodeEnum.Family -> {
                IntentFilter(Constant.BROADCAST_MESSAGE_FAMILY)
            }
            MessageCodeEnum.User -> {
                IntentFilter(Constant.BROADCAST_MESSAGE_USER)
            }
        }
        LocalBroadcastManager.getInstance(context).registerReceiver(broadcastReceiver, intentFilter)
    }

    fun unregisterBroadcast(
        context: Context,
        broadcastReceiver: BroadcastReceiver
    ) {
        LocalBroadcastManager.getInstance(context).unregisterReceiver(broadcastReceiver)
    }

}